package cn.cht.mvcframework.demo.pojo;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * 封装handler方法相关的信息
 */
public class Handler {
    /**
     * method.invoke(obj,)
     */
    private Object controller;

    private Method method;

    /**
     * 正则
     */
    private Pattern pattern;

    /**
     * 参数顺序
     */
    private Map<String ,Integer> paramIndexMapping;


    /**
     *  允许通过的 用户名
     */
    private Set<String> userNames;

    public Handler() {
    }

    public Handler(Object controller, Method method, Pattern pattern,Set<String> userNames) {
        this.controller = controller;
        this.method = method;
        this.pattern = pattern;
        this.paramIndexMapping = new HashMap<>();
        this.userNames=userNames;
    }

    public Set<String> getUserNames() {
        return userNames;
    }

    public void setUserNames(Set<String> userNames) {
        this.userNames = userNames;
    }

    public Object getController() {
        return controller;
    }

    public void setController(Object controller) {
        this.controller = controller;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public Pattern getPattern() {
        return pattern;
    }

    public void setPattern(Pattern pattern) {
        this.pattern = pattern;
    }

    public Map<String, Integer> getParamIndexMapping() {
        return paramIndexMapping;
    }

    public void setParamIndexMapping(Map<String, Integer> paramIndexMapping) {
        this.paramIndexMapping = paramIndexMapping;
    }


    // 验证 集合中是否存在白名单名字
    public void   WhiteList(String username) throws Exception {
        if (userNames == null ||  !userNames.contains(username) ){
            throw new Exception("该用户名:"+username+"没有访问权限!");
        }
    }
}
